// 组件接口

class TextMessage {
  constructor(message) {
    this.message = message;
  }
  getText() {
    return this.message;
  }
}

// 装饰器基类
class MessageDecorator {
  constructor(textMessage) {
    this.textMessage = textMessage;
  }
  getText() {
    return this.textMessage.getText();
  }
}
/**
 * HTML装饰器
 */
class HTMLDecorator extends MessageDecorator {
  getText() {
    const msg = super.getText();
    return `<p>${msg}</p>`;
  }
}
/**
 * 加密装饰器
 */
class EncryptDecorator extends MessageDecorator {
  getText() {
    const msg = super.getText();
    return this.encrypt(msg);
  }
  encrypt() {
    return msg.split("").reverse().join("");
  }
}

let message = new TextMessage("hello world");
let msg1 = new HTMLDecorator(message);
let msg2 = new EncryptDecorator(message);
console.log(message.getText());
console.log(msg1);
console.log(msg2);

// 也可以使用高阶函数 闭包的方式实现.
// introduce.js2
